package module.shop.view
{
	import com.greensock.*;
	import com.greensock.data.TweenLiteVars;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Expo;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;
	
	import main.AppCommonView;
	import main.AppControllerList;
	
	import module.main.event.MainEvent;
	
	public class ProduceItemEffect extends EventDispatcher
	{
		private var _x:Number;
		private var _y:Number;
		private var _spriteBg:Sprite;
		private var _resouseNum:Number;
		private var _type:String
		/**
		 *保存一个produceItemEffect的实例，通过调用start播放传入该实例的现实对象的动画效果 
		 * @param item  	显示对象
		 * @param resouseNum 资源数量
		 * @param x			显示对象的x值
		 * @param y			显示对象的y值
		 * @param tagetX	目标坐标y
		 * @param tagetY	目标坐标x
		 * @param type      资源种类
		 * 
		 */		
		public function ProduceItemEffect(item:Sprite,resouseNum:int,x:Number,y:Number,tagetX:Number,tagetY:Number,type:String)
		{
			_spriteBg = new Sprite;
			_type = type;
			_x = x;
			_y = y;
			_item = item;
			_resouseNum = resouseNum;
			_spriteBg.addChild(_item);
			_item.width = item.width;
			_item.height = item.height;
			_tagetX = tagetX;
			_tagetY = tagetY;
			_spriteBg.x = x;
			_spriteBg.y = y;
			isDone = true;
		}

		public function get spriteBg():Sprite
		{
			return _spriteBg;
		}

		public function start():void
		{
			_item.x = -_item.width / 2;
			_item.y = -_item.height;
			var filter:GlowFilter = new GlowFilter(0x000000,1);
			var filterArr:Array = new Array(filter);
			_item.filters = filterArr;
			TweenMax.to(_spriteBg, 0.8, {bezier:[{x:_x + 60, y:_y - 80}, {x:_x + 100  + int(Math.random()*10) * 5, y:_y + int(Math.random()*10) * 5}],onComplete:changeScale, ease:Cubic.easeIn});
			//TweenLite.to(_spriteBg,0.8,{x:_x + 60,y:_y - 40,onComplete:aa});
		}
		private function aa():void
		{
			//TweenLite.to(_spriteBg,1,{x:_x + 100  + int(Math.random()*10) * 5,y:_y + int(Math.random()*10) * 5,onComplete:changeScale,ease:Expo.easeIn})
		}
		private function changeScale():void
		{
			TweenLite.to(_spriteBg,0.2,{scaleY:0.6,onComplete:removeScale});
		}
		private function removeScale():void
		{
			TweenLite.to(_spriteBg,0.2,{scaleY:1,onComplete:addListen});
		}
		private var timer:Timer = new Timer(3000,1);
		private var _item:Sprite;
		private function addListen():void
		{
			_item.addEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,completeHandler);
		}
		private var isDone:Boolean;
		private function moveHandler(e:MouseEvent):void
		{
			if(isDone == true)
			{
				getItem();
				isDone = false;
			}
		}
		private function completeHandler(e:TimerEvent):void
		{
			if(isDone)
			{
				getItem();
				isDone = false;
			}
		}
		private var _tagetX:Number;
		private var _tagetY:Number;
		private function getItem():void
		{
			var $fiter:GlowFilter = new GlowFilter(0xffcccc,0.7,6,6,10,BitmapFilterQuality.MEDIUM,true);
			//_item.filters.push($fiter);
			var filterArr:Array = new Array($fiter);
			_item.filters = filterArr;
			_item.x = -_item.width / 2;
			_item.y = -_item.height / 2;
			if(_spriteBg.parent)
			{
				_spriteBg.parent.removeChild(_spriteBg);
			}
			AppControllerList.getInstance().appCommonView.mainView.addChild(_spriteBg);
			_spriteBg.x = _spriteBg.x + AppControllerList.getInstance().appCommonView.sceneView.followUpLayer.x;
			_spriteBg.y = _spriteBg.y + AppControllerList.getInstance().appCommonView.sceneView.followUpLayer.y;
			TweenLite.to(_spriteBg,0.5,{x:_tagetX,y:_tagetY,onComplete:removeItem});
		}
		private function removeItem():void
		{
			var gainResouceEvent:MainEvent = new MainEvent(MainEvent.GAIN_RESOUSE_EVENT);
			var resource:Object = new Object;
			resource.num = _resouseNum;
			resource.type = _type;
			gainResouceEvent.data = resource;
			_item.dispatchEvent(gainResouceEvent);
			TweenLite.to(_spriteBg, 0.5, {scaleX:5, scaleY:5, alpha:0,onComplete:removeAllChild});
		}
		private function removeAllChild():void
		{
			_item.removeEventListener(MouseEvent.MOUSE_MOVE,moveHandler);
			if(_spriteBg.parent)
			{
				_spriteBg.parent.removeChild(_spriteBg);
			}
			_item = null;
			_spriteBg = null;
		}
	}
}